package com.bw.unit3.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import java.lang.reflect.ParameterizedType

/**
 *
 * @ClassName: BaseFragment
 * @Description: java类作用描述
 * @Author: zhangyanchao
 * @CreateDate: 2024/9/13 11:01 AM
 * @Version: 1.0
 */
abstract class BaseFragment<VM : ViewModel, D : ViewDataBinding>(private val layoutId: Int) :
    Fragment() {

    protected lateinit var viewModel: VM
    protected lateinit var dataBinding: D
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        dataBinding = DataBindingUtil.inflate(inflater, layoutId, container, false)
        return dataBinding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val parameterizedType = javaClass.genericSuperclass as ParameterizedType
        val actualTypeArguments = parameterizedType.actualTypeArguments
        val clazz = actualTypeArguments[0] as Class<VM>
        viewModel = ViewModelProvider(this)[clazz]
        initView()
        initData()

    }

    abstract fun initData()

    abstract fun initView()
}